﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Api.Controllers.Web.Areas.Membership
{
    [EnsureHttps]
    public class DonationsController : Controller
    {
        public ActionResult SaveDonation(DataTransfer.Web.Areas.Membership.Donations.Donate.FormInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                if (Shared.Validation.Currency(input.Amount))
                {
                    if (input.Amount.ToDecimal() > 2500)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Amount",
                            Code = 27
                        });
                    }
                }
                else
                {
                    output.InvalidFields.Add(new DataTransfer.InvalidField()
                    {
                        Name = "Amount",
                        Code = 1
                    });
                }

                if (output.IsValid)
                {
                    var save = Shared.ObjectHelper.CopyObject<DataTransfer.Web.Areas.Membership.Donations.Donate.SaveInput>(input);

                    if (save.BucketID.ToInt() == 0)
                    {
                        save.BucketID = DataAccess.Donations.GetDefaultBucketID().ToString();
                    }

                    save.Status = Shared.Entities.Donations.Status.NotConfirmed; 
                    save.RecurringStartDate = DateTime.Now;

                    output.ID = DataAccess.Web.Areas.Membership.Donations.Donate.SaveDonation(save);

                    output.IsSaved = true;
                }
            }

            return new JsonXmlResult(output);
        }

        public ActionResult ConfirmPageLoad(DataTransfer.Web.Areas.Membership.Donations.Confirm.PageLoadInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = DataAccess.Web.Areas.Membership.Donations.Donate.ConfirmPageLoad(input);
            return new JsonXmlResult(model);
        }

        public ActionResult ConfirmDonation(DataTransfer.Web.Areas.Membership.Donations.Confirm.FormInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var save = Shared.ObjectHelper.CopyObject<DataTransfer.Web.Areas.Membership.Donations.Confirm.SaveInput>(input);

                save.Status = Shared.Entities.Donations.Status.Pending;

                DataAccess.Web.Areas.Membership.Donations.Donate.ConfirmDonation(save);

                output.IsSaved = true;

                if (input.Recurring.ToBool())
                {
                    Email.SendRecurringDonationEmail(input.DonationID.ToInt());
                }
                else
                {
                    Email.SendOneDonationEmail(input.DonationID.ToInt());
                }
            }

            return new JsonXmlResult(output);
        }
    }
}